#include "reportwidget.h"
#include "ui_reportwidget.h"
#include <QHeaderView>
#include <iostream>
#include <QFileDialog>
#include<QMessageBox>
#define MODELPATHS "../conf/default/report.conf"
#define GENPATH "/cache/tempreport/genreport.html"
#define DETAILPATH "/cache/tempreport/detailedreport.html"
#define FILEDETAILPATH "/cache/tempreport/file_detailedreport.html"
#define FILEGENPATH "/cache/tempreport/file_genreport.html"
#define FILEILLPATH "/cache/tempreport/file_illreport.html"
#define ILLGENPATH "/cache/tempreport/genillhtml.html"
#define XMLPATH "/cache/tempreport/xmlreport.xml"
#define PDFOUTPATH "/cache/tempreport/out.pdf"
ReportWidget::ReportWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ReportWidget)
{
    ui->setupUi(this);
    Init_Ui();
    for(int i=0;i<8;i++)
        connect(mbtn[i],&QPushButton::clicked,[this,i](){       //连接导出按钮
            this->Exportbtn_Click(i);
        });
}

ReportWidget::~ReportWidget()
{
    delete ui;
}

void ReportWidget::SetCurWidget(int i){
    ui->stackedWidget->setCurrentIndex(i);
    updatateweb(i);
}

void ReportWidget::Init_Ui(){
    apppath = qApp->applicationDirPath();//获取应用程序路径
    QTextStream& fstream=Yzty::GetTextSteam(MODELPATHS);
    QStringList items=fstream.readAll().split('#');
    items.removeFirst();
    auto&& pstruct=Yzty::ConfigParse_Struct(items);
    Init_Config(pstruct);
}
void ReportWidget::updatateweb(int code)
{
    QDir tmppath(apppath);
    tmppath.cdUp();
    switch ((Yzty::ReportToolCode)code) {
        case Yzty::ReportToolCode::QRcodereport:
            break;
        case Yzty::ReportToolCode::Xmlreport:
        m_webView[code]->setHtml("<b>XML报告已生成，点击右上角导出</b>");
            break;
        case Yzty::ReportToolCode::Genralreport:
            m_webView[code]->load(QUrl("file://"+tmppath.path()+QString(GENPATH)));
 //           m_webView[code]->load(QUrl("file://"+QString(GENPATH).arg(apppath).replace("/bin","")));
            break;
        case Yzty::ReportToolCode::Detailreport:
//            m_webView[code]->load(QUrl("file://"+QString(DETAILPATH).arg(apppath).replace("/bin","")));
            m_webView[code]->load(QUrl("file://"+tmppath.path()+QString(DETAILPATH)));
            break;
        case Yzty::ReportToolCode::Filedetailreport:
            m_webView[code]->load(QUrl("file://"+tmppath.path()+QString(FILEDETAILPATH)));
            break;
        case Yzty::ReportToolCode::FileGenreport:
            m_webView[code]->load(QUrl("file://"+tmppath.path()+QString(FILEGENPATH)));
            break;
        case Yzty::ReportToolCode::Illegalreport:
            m_webView[code]->load(QUrl("file://"+tmppath.path()+QString(ILLGENPATH)));
            break;
        case Yzty::ReportToolCode::Fileillegalreport:
            m_webView[code]->load(QUrl("file://"+tmppath.path()+QString(FILEILLPATH)));
            break;
        default:
            break;
    }


    for(int i=0;i<8;i++){
        m_webView[i]->reload();
    }
}

void ReportWidget::Init_Config(QMap<int,QVector<QPair<QString,QStringList>>>& pstruct){

    int n=pstruct.size();

    _models=QVector<QVector<QStandardItemModel*>>(n);
    _views=QVector<QVector<QTableView*>>(n);
    _plains=QVector<QVector<QPlainTextEdit*>>(n);

    for(int i=0;i<n;i++){

        //加入文件判断机制
        m_webView[i] = new QWebEngineView(this);
        QWidget* w=new QWidget(this);
        ui->stackedWidget->addWidget(w);
        QVBoxLayout* layout=new QVBoxLayout(w);
        QHBoxLayout* layout1 = new QHBoxLayout();
        layout1->addStretch(1);
        mbtn[i] = new QPushButton("导出");
        layout1->addWidget(mbtn[i]);
        layout->addLayout(layout1);
        layout->addWidget(m_webView[i]);
        w->setLayout(layout);
    }

}
void ReportWidget::Exportbtn_Click(int i) {//导出实现
    QString cmd="../YztyPy/bin/python3  ../YztyPy/htmltopdf.py %1 %2"; //外部Python命令
    QDir tmppath(apppath);
    tmppath.cdUp();
    QStringList reportname {    //默认文件名
        "/二维码.png",
        "/XML格式报告.xml",
        "/文件总体报告.html",
        "/文件违规报告.html",
        "/文件详细报告.html",
        "/总体检查报告.html",
        "/违规总体报告.html",
        "/详细检查报告.html"
    };
    QString defaultpath = QDir::homePath() + reportname[i]; //默认保存路径
    QString newpath = "";   //保存新路径
    QString oldpath = "";   //原路径
    QString pdfpath = tmppath.path() + QString(PDFOUTPATH);   //pdf输出路径

    if(i == 0) {
        newpath = QFileDialog::getSaveFileName(this,"保存文件",defaultpath,"PNG (*.png)");
    }
    else if(i == 1) {
        newpath = QFileDialog::getSaveFileName(this,"保存文件",defaultpath,"XML (*.xml)");
    }
    else
        newpath = QFileDialog::getSaveFileName(this,"保存文件",defaultpath,"HTML (*.html);;PDF (*.pdf)");
    if(newpath == "")
        return;
    switch ((Yzty::ReportToolCode)i) {//获取报告缓存路径

    case Yzty::ReportToolCode::QRcodereport:
        break;
    case Yzty::ReportToolCode::Xmlreport:
        oldpath = tmppath.path()+QString(XMLPATH);
//            QFile::copy(tmppath.path()+QString(XMLPATH),newpath);
        break;
    case Yzty::ReportToolCode::FileGenreport:
        oldpath = tmppath.path()+QString(FILEGENPATH);
        break;
    case Yzty::ReportToolCode::Fileillegalreport:
        oldpath = tmppath.path()+QString(FILEILLPATH);
        break;
    case Yzty::ReportToolCode::Filedetailreport:
        oldpath = tmppath.path()+QString(FILEDETAILPATH);
        break;
    case Yzty::ReportToolCode::Genralreport:
        oldpath = tmppath.path()+QString(GENPATH);
        break;
    case Yzty::ReportToolCode::Illegalreport:
        oldpath = tmppath.path()+QString(ILLGENPATH);
        break;
    case Yzty::ReportToolCode::Detailreport:
        oldpath = tmppath.path()+QString(DETAILPATH);
        break;
    default:
        break;
    }
    if(!(newpath.endsWith(".html")||newpath.endsWith(".xml"))){     //若选择非HTML,XML
        if(!newpath.endsWith(".pdf")) { //如果没有输入后缀名，自动为PDF
            newpath.append(".pdf");
        }
        cmd = cmd.arg(oldpath,pdfpath);
        system(cmd.toLocal8Bit()); //运行外部Python文件，生成PDF
        oldpath = pdfpath;
    }
    QFile file(newpath);
    if(file.exists()) { //检测文件是否已经存在，删除后再复制
        file.remove();
    }
    if(QFile::copy(oldpath,newpath))
    {
        QMessageBox::information(this,"导出结果","导出完成");
    }
    else {
        QMessageBox::information(this,"导出结果","导出失败");
    }
}
